package com.genesis.yunnanji.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.genesis.yunnanji.R;
import com.genesis.yunnanji.activity.WebActivity;
import com.genesis.yunnanji.adapter.NewsAdapter;
import com.genesis.yunnanji.bean.NewsBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.utils.GenesisUtils;
import com.genesis.yunnanji.utils.NetWorkUtils;
import com.google.gson.Gson;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/8.
 */
public class Video extends Fragment {
    private View view;
    private RecyclerView mRecyclerView;
    private TwinklingRefreshLayout refreshLayout;
    private int page = 1;
    private List<NewsBean> newsBeanList;
    private NewsAdapter adapter;
    private GenesisUtils genesisUtils;

    public static Video newInstance(String param) {
        Video obj = new Video();
        Bundle bundle = new Bundle();
        bundle.putString("args1", param);
        obj.setArguments(bundle);
        return obj;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = this.getActivity().getLayoutInflater().inflate(R.layout.fragment_video, null);
            genesisUtils = GenesisUtils.getIntance();
            mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_video_list);
            refreshLayout = (TwinklingRefreshLayout) view.findViewById(R.id.bga_video_refresh);
            initView();
            getData(null);
        } else {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
        }
        return view;
    }

    protected void initView() {
        newsBeanList = new ArrayList<>();
        LoadingView loadingView = new LoadingView(this.getActivity());
        refreshLayout.setBottomView(loadingView);
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                page = 1;
                newsBeanList.removeAll(newsBeanList);
                getData(refreshLayout);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                page += 1;
                getData(refreshLayout);
            }
        });

    }

    protected void setUpView() {

    }

    private void getData(final TwinklingRefreshLayout refreshLayout) {
        RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.NEWS_LIST);
        params.addBodyParameter("categoryid", 89 + "");
        params.addBodyParameter("p", page + "");
        NetWorkUtils.doCacheGet(params, new NetWorkUtils.HttpResultListener() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                Gson gson = new Gson();
                try {
                    JSONArray jsonArray = jsonObject.getJSONObject("result").getJSONArray("list");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        NewsBean newsBean = gson.fromJson(jsonArray.get(i).toString(), NewsBean.class);
                        newsBeanList.add(newsBean);
                    }
                    adapter = new NewsAdapter(Video.this.getActivity(), newsBeanList, true);
                    mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(Video.this.getActivity()).size((int) getResources().getDimension(R.dimen.x1)).color(Color.parseColor("#f5f5f5")).margin((int) getResources().getDimension(R.dimen.x1)).build());
                    mRecyclerView.setLayoutManager(new LinearLayoutManager(Video.this.getActivity()));
                    mRecyclerView.setNestedScrollingEnabled(false);
                    mRecyclerView.setAdapter(adapter);
                    adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                            Intent intent = new Intent(Video.this.getContext(), WebActivity.class);
                            intent.putExtra("url", newsBeanList.get(position).getTaget_link() + "?__apptoken__=" + genesisUtils.getAppToken()+ "&from=app");
                            startActivity(intent);
                        }
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void OnCached(JSONObject jsonObject) {
            }

            @Override
            public void onFinished() {
                if (refreshLayout != null) {
                    refreshLayout.finishRefreshing();
                    refreshLayout.finishLoadmore();
                }
            }

            @Override
            public void onError(String errorStr) {

            }
        });
    }
}
